package com.drinks.yiniang.utils;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;

import com.drinks.yiniang.entity.category.CategoryGoodsJson;
import com.drinks.yiniang.entity.category.CategoryJson;
import com.drinks.yiniang.entity.goods.GoodsDetailJson;
import com.drinks.yiniang.entity.goods.SearchGoodsJson;
import com.drinks.yiniang.entity.photo.Photos;
import com.drinks.yiniang.entity.shop.ShoppingCartJson;
import com.drinks.yiniang.entity.user.order.OrderDetailJson;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.LogUtils;

import java.util.List;

/**
 * Created by han on 2019/6/20 0020.
 */

public class RecyclerUtils {

    public static LinearLayoutManager getHorizontalLinearLayout(Context mContext) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        return linearLayoutManager;
    }

    public static GridLayoutManager getMuiltGridLayout(Context mContext, final int spaceCount, final int limitCount) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, spaceCount);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position > limitCount ? spaceCount : 1;
            }
        });


        return layoutManager;
    }

    public static GridLayoutManager getVerticalGridLayout(Context mContext, final int spaceCount) {

        return getGridLayout(mContext, spaceCount, LinearLayoutManager.VERTICAL);
    }

    public static GridLayoutManager getGridLayout(Context mContext, final int spaceCount, int orientation) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, spaceCount);

        layoutManager.setOrientation(orientation);

        return layoutManager;
    }

    public static GridLayoutManager getCategoryGoodsGridLayout(Context mContext, final List<CategoryGoodsJson.CategoryGoodsList> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //去掉header view
                int pos = position - 1;
                int count = !CommonUtils.isArrayIndexOutOfBounds(list, pos) ? (list.get(pos).getItemType() == CategoryGoodsJson.ITEM_TYPE_CATEGORY_HOT_GOODS ? 1 : 3) : 3;

                return count;
            }
        });

        return layoutManager;
    }

    public static GridLayoutManager getShoppingCartGridLayout(Context mContext, final List<ShoppingCartJson.ShoppingCartList> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //去掉header view
                int pos = position - 1;
                int count = !CommonUtils.isArrayIndexOutOfBounds(list, pos) ? (list.get(pos).getItemType() == ShoppingCartJson.ITEM_TYPE_CART_GOODS ? 2 : list.get(pos).getItemType() == ShoppingCartJson.ITEM_TYPE_CART_RECOMMEND_TITLE ? 2 : 1) : 2;

                return count;
            }
        });

        return layoutManager;
    }

    public static GridLayoutManager getSearchGoodsGridLayout(Context mContext, final List<SearchGoodsJson.SearchGoodsList> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //去掉header view
                int pos = position - 1;
                int count = !CommonUtils.isArrayIndexOutOfBounds(list, pos) ? (list.get(pos).getItemType() == SearchGoodsJson.ITEM_TYPE_SEARCH_GOODS_LIST ? 1 : list.get(pos).getItemType() == SearchGoodsJson.ITEM_TYPE_SEARCH_GOODS_TITLE ? 2 : 2) : 2;

                return count;
            }
        });

        return layoutManager;
    }

    public static GridLayoutManager getGoodsDetailGridLayout(Context mContext, final List<GoodsDetailJson.GoodsDetailList> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //去掉header view
                int pos = position - 1;
                int count = !CommonUtils.isArrayIndexOutOfBounds(list, pos) ?
                        (list.get(pos).getItemType() == GoodsDetailJson.ITEM_TYPE_GOODS_COMMENT ? 2 :
                                list.get(pos).getItemType() == GoodsDetailJson.ITEM_TYPE_GOODS_WEB ? 2 :
                                        list.get(pos).getItemType() == GoodsDetailJson.ITEM_TYPE_GOODS_RECOMMENT_TITLE ? 2 :
                                                list.get(pos).getItemType() == GoodsDetailJson.ITEM_TYPE_GOODS_RECOMMENT_LIST ? 1 : 2) : 2;

                return count;
            }
        });

        return layoutManager;
    }

    public static GridLayoutManager getOrderDetailGridLayout(Context mContext, final List<OrderDetailJson.OrderDetailList> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //去掉header view
                int pos = position - 1;
                int count = !CommonUtils.isArrayIndexOutOfBounds(list, pos) ?
                        (list.get(pos).getItemType() == OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_LIST ? 2 :
                                list.get(pos).getItemType() == OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_TOTAL ? 2 :
                                        list.get(pos).getItemType() == OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_LOGISTICS ? 2 :
                                                list.get(pos).getItemType() == OrderDetailJson.ITEM_TYPE_ORDER_DETAIL_RECOMMEND ? 1 : 2) : 2;

                return count;
            }
        });

        return layoutManager;
    }

    public static GridLayoutManager getPayResultGoodsGridLayout(Context mContext) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //去掉header view
                int pos = position - 1;
                int count = pos >= 0 ? 1 : 2;

                return count;
            }
        });

        return layoutManager;
    }

    public static GridLayoutManager getAlbumPhotoGridLayout(Context mContext, final List<Photos> list) {
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                //去掉header view
                int pos = position - 1;
                //int count = !CommonUtils.isArrayIndexOutOfBounds(list, pos) ? (list.get(pos).getItemType() == Photos.TYPE_ITEM_PHOTO_ALBUM_TITLE ? 3 : 1) : 3;

                return 3;
            }
        });

        return layoutManager;
    }
}
